package com.yuqih.common.websocket.monitor;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yuqih.common.exception.CodedException;

public class MonitorAction {

	

	private String uri;

	// 通过url取得下面的action和params
	@JsonIgnore
	private String action;
	@JsonIgnore
	private Map<String, Object> params;

	// 通过url获得的
	@JsonIgnore
	private Object condition;

	/**
	 * 
	 */
	public MonitorAction() {
		super();
	}

	/**
	 * 
	 */
	MonitorAction(String uri) {
		super();
		this.uri = uri;
		UriComponents uriComponents = UriComponentsBuilder.fromUriString(uri).build();
		this.action = uriComponents.getPath();
		Map<String, Object> map = new HashMap<String, Object>();
		MultiValueMap<String, String> queryParams = uriComponents.getQueryParams();
		for (String k : queryParams.keySet()) {
			try {
				String value = queryParams.getFirst(k);
				if (StringUtils.isBlank(value)) {
					continue;
				}
				map.put(k, URLDecoder.decode(value, StandardCharsets.UTF_8.name()));
			} catch (UnsupportedEncodingException e) {
				throw CodedException.from(e);
			}
		}
		if (map.isEmpty()) {
			this.params = null;
		} else {
			this.params = map;
		}

	}

	/**
	 * @return the action
	 */
	public String getAction() {
		return action;
	}

	/**
	 * @param action
	 *            the action to set
	 */
	public void setAction(String action) {
		this.action = action;
	}

	/**
	 * @return the params
	 */
	public Map<String, Object> getParams() {
		return params;
	}

	/**
	 * @param params
	 *            the params to set
	 */
	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	/**
	 * @return the condition
	 */
	public Object getCondition() {
		return condition;
	}

	/**
	 * @param condition
	 *            the condition to set
	 */
	public void setCondition(Object condition) {
		this.condition = condition;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "MonitorAction [action=" + action + ", params=" + params + "]";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((action == null) ? 0 : action.hashCode());
		result = prime * result + ((params == null) ? 0 : params.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MonitorAction other = (MonitorAction) obj;
		if (action == null) {
			if (other.action != null)
				return false;
		} else if (!action.equals(other.action))
			return false;
		if (params == null) {
			if (other.params != null)
				return false;
		} else if (!params.equals(other.params))
			return false;
		return true;
	}

	/**
	 * @return the uri
	 */
	public String getUri() {
		return uri;
	}

	/**
	 * @param uri
	 *            the uri to set
	 */
	public void setUri(String uri) {
		this.uri = uri;
	}

}
